# -*- coding: utf-8 -*-
# @Time    : 2024/9/22 22:54
# @Author  : Alvin
# @File    : ApiLogisticsInfo.py
# @Des     :
import json
import logging
import traceback

from tornado.concurrent import run_on_executor
from tornado.gen import coroutine

from common.http_serv import app
from server.base_request import BaseHttpHandler
from server.handler.LogisticsInfoHandler import LogisticsInfoHandler


@app.route("/getLogisticsNum")
class ApiGetLogisticsNum(BaseHttpHandler):
    """
         获取车氏物流单号
        """

    @coroutine
    def post(self):
        """
        post消息处理
        :return:
        """
        data = yield self.post_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def post_async(self):
        """
        post_async消息处理
        :return:
        """
        try:
            rec_data = json.loads(self.request.body.decode())
            logging.info(f"创建订单原始参数：{rec_data}")
            handler = LogisticsInfoHandler()
            result = handler.handle_create_logistics_num(rec_data)
            logging.info(f"获取物流单号参数：{rec_data},响应：{result}")
            return {"rsp": result}
        except Exception as ex:
            logging.error("getLogisticsNum ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)


@app.route("/getMailNoGiftDetail")
class ApiGetLogisticsNum(BaseHttpHandler):
    """
    获取物流订单信息列表
    """

    @coroutine
    def get(self):
        data = yield self.get_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def get_async(self):
        """
        get消息处理
        :return:
        """
        try:
            arguments = self.request.arguments
            logging.info(f"获取物流订单列表参数：{arguments}")
            userId = self.get_query_argument("userId")
            logisticsNo = self.get_query_argument("mailNoGift")
            hanlder = LogisticsInfoHandler()
            rsp = hanlder.get_mail_no_gift_detail(userId, logisticsNo)
            return {"rsp": rsp}
        except Exception as ex:
            logging.error("getLogisticsOrderList ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)


@app.route("/getUserInfo")
class ApiGetUserInfo(BaseHttpHandler):

    @coroutine
    def get(self):
        data = yield self.get_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def get_async(self):
        """
        get消息处理
        :return:
        """
        try:
            arguments = self.request.arguments
            logging.info(f"获取用户信息：{arguments}")
            userId = self.get_query_argument("userId")
            hanlder = LogisticsInfoHandler()
            rsp = hanlder.get_custom_user_info(userId)
            return {"rsp": rsp}
        except Exception as ex:
            logging.error("getUserInfo ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)


@app.route("/updateLogisticsNoPickingCode")
class ApiUpdateLogisticsNoPickingCode(BaseHttpHandler):
    """
         更新取件码
    """

    @coroutine
    def post(self):
        """
        post消息处理
        :return:
        """
        data = yield self.post_async()
        self.write(data)
        self.finish()

    @run_on_executor
    def post_async(self):
        """
        post_async消息处理
        :return:
        """
        try:
            rec_data = json.loads(self.request.body.decode())
            handler = LogisticsInfoHandler()
            result = handler.handle_update_logistics_no_picking_code(param=rec_data)
            logging.info(f"更新单号取件码参数：{rec_data},响应：{rec_data}")
            return {"rsp": result}
        except Exception as ex:
            logging.error("updateLogisticsNoPickingCode ex:%s", ex)
            logging.error("traceback.format_exc():\n%s" % traceback.format_exc())
            return str(ex)
